package com.example.mywechat.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mywechat.MainActivity;
import com.example.mywechat.model.ChatListModel;
import com.example.mywechat.R;

import java.util.List;

public class ChatListAdapter extends BaseAdapter {
    Context myContent;
    int myItemLayout;
    List<ChatListModel> myListData;

    public ChatListAdapter(MainActivity myContent, int myItemLayout, List<ChatListModel> myListData) {
        this.myContent = myContent;
        this.myItemLayout = myItemLayout;
        this.myListData = myListData;
    }

    @Override
    public int getCount() {
        return myListData.size();
    }

    @Override
    public Object getItem(int i) {
        return myListData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View myView = View.inflate(myContent, myItemLayout, null);
        ImageView headImage = myView.findViewById(R.id.headImage);
        TextView name = myView.findViewById(R.id.name);
        TextView message = myView.findViewById(R.id.message);
        TextView date = myView.findViewById(R.id.tv_date);
        //要给控件添加数据，获取数据，通过friend类实例化对象
        ChatListModel myChat = myListData.get(i);
        headImage.setImageResource(myChat.getHeadImage());
        name.setText(myChat.getName());
        message.setText(myChat.getMessage());
        date.setText(myChat.getDate());
        return myView;
    }
}
